#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        unordered_map<int, int> m;
        m[0] = -1;

        int sum = 0, countK = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            sum += nums[i] == 0 ? -1 : 1;
            if (m.count(sum))
                countK = max(countK, i - m[sum]);
            else
                m[sum] = i;
        }
        return countK;
    }
};